const $dataIntegration = require('w-vue-chongqing/src/api/dataIntegration');
const $modelService = require('w-vue-chongqing/src/api/dataModel');
const scheduleModule = require("../scheduleModule").default;
const transferModule = require("../transferModule").default;
const $config = require("../config");

export default {
  components: {
    scheduleModule,
    transferModule,
  },
  props: {
    inteScheduleInfo: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      modelInfo: {}, // 模型信息
      targetColumns: [], // 目标表的字段
      scheduleJob: {}, // 调度信息
      jobInfo: {}, // 作业信息
      transferJobs: [], // 转换信息
    };
  },

  created() {
    // 编辑进入
    if(this.inteScheduleInfo.id) {
      this.modelInfo = { ...this.inteScheduleInfo.tableDto };
      this.getHdwInfoByJob();
    }
  },
  methods: {
    /**
     * @Author: y_zp
     * @description: 校验数据
     */
    toValidate(callback) {
      let jobValidate = false;
      this.$refs.transferModule.toValidate(data => {
        jobValidate = data;
      });

      let scheduleValidate = false;
      this.$refs.scheduleModule.toValidate(data => {
        scheduleValidate = data;
      });

      return jobValidate && scheduleValidate;
    },
    /**
     * @Author: y_zp
     * @description: 获取最终提交的数据
     */
    toValidateData() {
      let scheduleParams = this.$refs.scheduleModule.toValidateData();
      let jobParams = this.$refs.transferModule.toValidateData();

      let params = {
        ...jobParams,
        scheduleJob: scheduleParams,
        tableDto: this.modelInfo
      }
      return params;
    },

    /**
     * @Author: y_zp
     * @description: 通过集成作业获取信息
     */
    async getHdwInfoByJob() {
      this.loading = true;
      let { data: info } = await $dataIntegration.getHdwInfoByJob({ ...this.inteScheduleInfo });
      let { data: modelDetail } = await $modelService.getIndicatorTableDataById({ id: info.table.id });
      // 目标表的信息
      this.modelInfo = { ...info.table };
      this.targetColumns = modelDetail.columnList;
      this.jobInfo = info.transJob;
      this.scheduleJob = info.transJob.scheduleJob;
      this.transferJobs = info.transJob.transferJobs;
      this.loading = false;
    },

    async changeModel(value) {
      this.loading = true;
      // 目标表的信息
      let { data: modelDetail } = await $modelService.getIndicatorTableDataById({ id: value.id });

      this.modelInfo = modelDetail;
      this.targetColumns = modelDetail.columnList.map(item => {
        let desc = item.columnName;
        let name = item.columnCode;
        return {
          ...item,
          description: desc,
          columnName: name
        }
      });

      $dataIntegration .getHdwTableJob({ ...modelDetail }).then(req => {
        const info = req.data;
        this.jobInfo = info;
        this.scheduleJob = info.scheduleJob;
        this.transferJobs = info.transferJobs;

        if(!this.modelInfo.datasourceName) {
          this.modelInfo.datasourceName = info.tableDto.datasourceName;
        }
        
      })
      .finally(req => {
        this.loading = false;
      });
    },

    toDebug() {
      this.$refs.transferModule.toDebug();
    },


  }
};